from method import Method, Param, String
from session import Session, User

class LoginMethod(Method):
    def on_success(self, smuggler, response):
        smuggler.session = Session(response.Login['Session']['id'])
        return response    

class WithPassword(LoginMethod):
    """
    This method establishes a session and logs a user in based on the specified
    email address and password.

    Arguments:
    String EmailAddress
    String Password
    
    >>> withPassword = WithPassword()
    >>> request = withPassword(None, 'Test', password='test', APIKey='123456')
    >>> request.method == 'smugmug.login.withPassword'
    True
    >>> request.params == dict(EmailAddress='Test', Password='test', APIKey='123456')
    True
    """

    name = 'smugmug.login.withPassword'
    params = (
        Param('EmailAddress', String),
        Param('Password', String),
        Param('APIKey', String),
    )
    useSsl = True
    responseKey = 'Login'

withPassword = WithPassword()

class WithHash(LoginMethod):
    """
    This method establishes a session and logs in with a stored hash, rather
    than the real password. Slightly more secure than logging in with a
    plaintext password, it's still recommended that this step occur via SSL
    whenever possible.

    Arguments:
    String UserID
    String PasswordHash
    
    >>> withHash = WithHash()
    >>> request = withHash(None, 8910, passwordHash='test', APIKey='123456')
    >>> request.method == 'smugmug.login.withHash'
    True
    >>> request.params == dict(UserID=8910, PasswordHash='test', APIKey='123456')
    True
    """

    name = 'smugmug.login.withHash'
    params = (
        Param('UserID', String),
        Param('PasswordHash', String),
        Param('APIKey', String),        
    )
    useSsl = True
    responseKey = 'Login'

withHash = WithHash()

class Anonymously(LoginMethod):
    """
    This method establishes a session which isn't logged in.
    
    >>> anonymously = Anonymously()
    >>> request = anonymously(None, apikey='123456')
    >>> request.method == 'smugmug.login.anonymously'
    True
    >>> request.params == dict(APIKey='123456')
    True
    """

    name = 'smugmug.login.anonymously'
    params = (
        Param('APIKey', String),        
    )
    useSsl = True
    responseKey = 'Login'

anonymously = Anonymously()

if __name__ == '__main__':
    import doctest
    doctest.testmod()